package com.tusuo.stds.util;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import android.util.Base64;

/**
 *
 * 3DES加密工具类
 */

public class DESedeUtil {

    // 密钥算法
    private static final String KEY_ALGORITHM = "DESede";
    // 加密/解密算法    工作模式    填充模式
    private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
    // 密钥 长度不得小于24
//    private final static String secretKey = "123456789012345678901234" ;
    // 向量 可有可无 终端后台也要约定
    private final static String iv = "" ;
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8" ;

    public static String getSecretKey(String src) {
        // Cookie格式: TSManage:xxxxxx;
        // TODO: get TSManage key value
        if (src != null && src.length() > 24) {
            return src.substring(src.length() - 25, src.length() - 1);
        }

        return "";
    }

    /**
     * 加密
     * @param data 待加密字符串
     * @param key 加密私钥，长度不能够小于8位
     * @return 加密后的字节数组，一般结合Base64编码使用
     */
    private String encode(String key, String data) throws Exception {
//        return encode(key, data.getBytes());
        return encode(key, data.getBytes(encoding));
    }

    /**
     * 加密
     * @param data 待加密字符串
     * @param key 加密私钥，长度不能够小于8位
     * @return 加密后的字节数组，一般结合Base64编码使用
     */
    private static String encode(String key, byte[] data) throws Exception {
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
//        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ips);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] bytes = cipher.doFinal(data);
//        return Base64.encodeToString(bytes, 3);
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    /**
     * 解密
     * @param data 待解密字符串
     * @param key 解密私钥
     * @return 解密后的字节数组
     */
    private static byte[] decode(String key, byte[] data) throws Exception {
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
//        cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    /**
     * 获取编码后的值
     * 由于多线程的原因，需要使用synchronized
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static synchronized String decodeValue(String key, String data) {
        byte[] dataBytes;
        String value;
        try {
//            dataBytes = decode(key, Base64.decode(data, 3));
            dataBytes = decode(key, Base64.decode(data, Base64.DEFAULT));
//            value = new String(dataBytes);
            value = new String(dataBytes, encoding);
        } catch (Exception e) {
            value = "";
        }
        return value;
    }
}
